<template>
  <div class="app">
    <div class="title">
      <el-input v-model="topic.title" placeholder="请输入标题"></el-input>
    </div>
    <Editor
      v-model="topic.content"
      :content="topic.content"
      :isCreate="isCreate"
    ></Editor>
    <div class="commitBox">
      <el-button @click="btnCommit" :type="isCreate ? 'primary' : 'success'">{{
        isCreate ? '发布' : '修改'
      }}</el-button>
      <el-select v-model="topic.type" placeholder="请选择分类">
        <el-option
          v-for="item in types"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <div class="switch">
        <el-switch
          v-model="topic.state"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
          active-text="公开"
          inactive-text="私密"
        >
        </el-switch>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getTopicTypes,
  createTopic,
  getTopicById,
  modifyTopic,
} from '../api/topic';
import Editor from '@/components/Editor';

export default {
  components: {
    Editor,
  },
  data() {
    return {
      topic: {
        title: '',
        content: '',
        type: '',
        state: 1,
      },
      types: [],
      isCreate: true,
    };
  },
  methods: {
    async getTopicTypes() {
      const res = await getTopicTypes();
      if (res.code != 200) {
        this.$message.error('获取分类列表错误，请刷新页面');
      }
      this.types = res.data;
    },
    async btnCommit() {
      if (this.topic.type == '' || !this.topic.type) {
        return this.$message.error('请选择分类');
      }
      if (this.topic.content == '' || !this.topic.content)
        return this.$message.error('请输入内容');
      if (this.isCreate) {
        const res = await createTopic(this.topic);
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: '发布文章成功',
          });
          this.$router.push('/user/' + this.$store.state.user_id);
        }
      } else {
        this.topic.topic_id = this.$route.params.topic_id;
        const res = await modifyTopic(this.topic);
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: '修改文章成功',
          });
          this.$router.push('/user/' + this.$store.state.user_id);
        }
      }
    },
    async getTopicById(topic_id) {
      const result = await getTopicById(topic_id);
      for (const key in this.topic) {
        this.topic[key] = result.data[key];
      }
    },
  },
  created() {
    if (this.$route.params.update) {
      this.isCreate = false;
      this.getTopicById(this.$route.params.topic_id);
    } else {
      this.isCreate = true;
    }
    this.getTopicTypes();
  },
};
</script>

<style lang="less" scoped>
.app {
  .title {
    width: 1050px;
    margin: auto;
    padding: 0 32px;
    padding-top: 32px;
    background-color: #fff;
    border-radius: 4px 4px 0 0;
    min-height: 112px;
    margin-top: 20px;
    padding-bottom: 24px;
    box-sizing: border-box;
  }
  .commitBox {
    width: 1050px;
    margin: auto;
    padding: 10px 30px;
    box-sizing: border-box;
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    background-color: #fff;
    .el-button {
      margin-left: 30px;
    }
    .switch {
      margin-right: auto;
    }
  }
}
</style>
